{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2b8813da",
   "metadata": {},
   "source": [
    "------\n",
    "* 学习人： 周晓源\n",
    "* 时间：2021年11月15日\n",
    "* week12\n",
    "* 学习线上资源：[python官方文档](https://docs.python.org)\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d59be2ba",
   "metadata": {},
   "source": [
    "## 本周\n",
    "\n",
    "### 字典的一般方法 \n",
    "\n",
    "> 1. 字典的查询 查询key ==> value\n",
    ">> 1. 先看字典是否存在：in 查询成员是否存在的重要方法\n",
    ">> 2. 查询\n",
    "> 2. 字典的修改\n",
    "> 3. 字典的删除\n",
    "> 4. 字典的增加（ dict_name[key] = value ）\n",
    "\n",
    "### 字典的进阶方法\n",
    "\n",
    "> 1. 字典的初始化\n",
    ">> 1. D.fromkeys()\n",
    ">> 2. D.setdefault()\n",
    "\n",
    "### 字典的实践（一）课本\n",
    "\n",
    "### 字典的实践（二）课本\n",
    "\n",
    "### 字典的实践（三）课本"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "739a4e44",
   "metadata": {},
   "source": [
    "----------------------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40dd9e27",
   "metadata": {},
   "source": [
    "## 1. 字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "002a1b82",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T06:55:31.279491Z",
     "start_time": "2021-11-15T06:55:31.263491Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFang', 'stu1803': 'XiaoBao'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFang\",\n",
    "    \"stu1803\":\"XiaoBao\"\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ccfe7d",
   "metadata": {},
   "source": [
    "### 1.查询\n",
    "\n",
    ">>> * 直接查找dict_name 当关键词key不存在时会报错：KeyError: 'stu1804'\n",
    ">>> * get()方法D.get(k[,d])当关键词key不存在时，则会出示None，不会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "65bc0662",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:02:06.917845Z",
     "start_time": "2021-11-15T07:02:06.909846Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接查找 dict_name[key]   当关键词key不存在时会报错：KeyError: 'stu1804'\n",
    "stu_info[\"stu1801\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9958b9c8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:03:27.638132Z",
     "start_time": "2021-11-15T07:03:27.622131Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZiQi\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# 使用get方法D.get(k[,d])查找     当关键词key不存在时，则会出示None，不会报错\n",
    "print(stu_info.get(\"stu1801\"))\n",
    "print(stu_info.get(\"stu1804\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adefba23",
   "metadata": {},
   "source": [
    "### 2.修改\n",
    ">>> * 先查询 查看key是否存在 使用 in  来查询成员\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "418ea248",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:07:36.056885Z",
     "start_time": "2021-11-15T07:07:36.044618Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询存在stu_info的\n",
    "\"stu1803\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "87ee3729",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:08:07.621139Z",
     "start_time": "2021-11-15T07:08:07.613140Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询不存在stu_info的\n",
    "\"stu1804\" in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "609a8575",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:10:41.450331Z",
     "start_time": "2021-11-15T07:10:41.442332Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFang', 'stu1803': 'WeiBao'}\n"
     ]
    }
   ],
   "source": [
    "# 如果key存在 修改  \"stu1803\":\"XiaoBao\" ==>  \"stu1803\":\"WeiBao\"\n",
    "if \"stu1803\" in stu_info:\n",
    "     stu_info[\"stu1803\"] = \"WeiBao\"\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20f0dc03",
   "metadata": {},
   "source": [
    "### 3. 字典的删除\n",
    ">> * D.pop()的操作为：\n",
    ">>> * 先查询是否存在 if in\n",
    ">>> * 然后使用删除的方法\n",
    ">> * D.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b8e61f02",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:26:19.633622Z",
     "start_time": "2021-11-15T07:26:19.618618Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFang'}\n"
     ]
    }
   ],
   "source": [
    "if 'stu1803' in stu_info:\n",
    "    stu_info.pop('stu1803')\n",
    "print(stu_info)  # 如果不先查询的话有可能会报错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae8b0250",
   "metadata": {},
   "source": [
    "---------\n",
    "##### pop删除的错误示范："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1e09ef95",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:30:12.669962Z",
     "start_time": "2021-11-15T07:30:12.650342Z"
    }
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'stu1084'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ZHOUXI~1\\AppData\\Local\\Temp/ipykernel_13656/2462762759.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'stu1084'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'stu1084'"
     ]
    }
   ],
   "source": [
    "stu_info.pop('stu1084')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f1c253bd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:30:54.411639Z",
     "start_time": "2021-11-15T07:30:54.387594Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "pop expected at least 1 argument, got 0",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mC:\\Users\\ZHOUXI~1\\AppData\\Local\\Temp/ipykernel_13656/474395142.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mstu_info\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: pop expected at least 1 argument, got 0"
     ]
    }
   ],
   "source": [
    "stu_info.pop()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bd5de99",
   "metadata": {},
   "source": [
    "##### -------------------pop删除错误示范结束--------------------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d928a5a7",
   "metadata": {},
   "source": [
    "### "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "778acb5c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:31:52.035454Z",
     "start_time": "2021-11-15T07:31:52.027465Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d4e32550",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:35:36.830528Z",
     "start_time": "2021-11-15T07:35:36.823356Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1802', 'XiaoFang')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "df0ba0e8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:35:54.834446Z",
     "start_time": "2021-11-15T07:35:54.813559Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi'}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80a8f53d",
   "metadata": {},
   "source": [
    "课本 p112"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5d7d24fb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-11-15T07:48:51.438818Z",
     "start_time": "2021-11-15T07:48:47.886664Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hello\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 1, 'u': 0}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 目标 ： 查询 元音字母是否出现在word文本当中 ，并且统计出现的次数\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "found = {}\n",
    "\n",
    "for i in vowels:\n",
    "    found[i] = 0\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3903dc90",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
